package yxy.game.pm2.command.yilingbattle;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.module.rank.RankEntry;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.YiLingMessage;
import yxy.game.pm2.module.player.PlayerHandle;
import yxy.game.pm2.module.yiling.RankYiLingBattle;

import java.util.Collection;


/****************************************
 * @name CMD_YiLingBattleRank
 * @description 夷陵之战 通关进度排行榜
 * @author 梁健彬
 * @since 2022-05-17
 ***************************************/
@MessageType(proto = Command.YiLingBattleRank, classtype = YiLingMessage.YiLingBattleRankRequest.class)
public class CMD_YiLingBattleRank extends Command<YiLingMessage.YiLingBattleRankRequest> {

    @Override
    public void execute(MessageSource messageSource, YiLingMessage.YiLingBattleRankRequest request) {
        //todo
        String playerCode = messageSource.getTag();
        YiLingMessage.YiLingBattleRankResponse.Builder response = YiLingMessage.YiLingBattleRankResponse.newBuilder();

        RankYiLingBattle rankYiLing = RankYiLingBattle.getRank(playerCode);
        try {
            PlayerHandle playerHandle = new PlayerHandle();

            Collection<RankEntry> top = rankYiLing.top(rankYiLing.topSize());
            int rank = 0;
            for (RankEntry entry : top) {
                String pCode = entry.getKey();
                int floor = entry.getValue().intValue();

                YiLingMessage.RankTower.Builder builder = YiLingMessage.RankTower.newBuilder();
                builder.setRank(++rank);
                builder.setPlayerInfo(playerHandle.playerSimpleInfo(pCode));
                builder.setFloor(floor);
                response.addRank(builder);
            }

            {
                int playerRank = 0;
                int floor = 0;
                RankEntry entry = rankYiLing.get(playerCode);
                if (entry != null) {
                    playerRank = rankYiLing.rank(playerCode);
                    floor = entry.getValue().intValue();
                }

                YiLingMessage.RankTower.Builder builder = YiLingMessage.RankTower.newBuilder();
                builder.setRank(playerRank);
                builder.setPlayerInfo(playerHandle.playerSimpleInfo(playerCode));
                builder.setFloor(floor);
                response.setMy(builder);
            }


            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }


    }

    private void send(MessageSource messageSource, YiLingMessage.YiLingBattleRankResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}